package com.wei.czz.framework.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wei.czz.common.enums.CommonEnum;
import com.wei.czz.common.utils.SecurityUtils;
import com.wei.czz.framework.blog.dao.BlogAuditUserConfigDao;
import com.wei.czz.framework.blog.entity.BlogAuditUserConfigEntity;
import com.wei.czz.framework.blog.service.BlogAuditUserConfigService;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 * Created by IntelliJ IDEA.
 * 
 * @author wyw
 * date: 2025-01-17 10:17:19
 * className: BlogAuditUserConfigServiceImpl 博客审核用户配置服务实现类
 * version: 1.0
 * description:
 */
@Service("blogAuditUserConfigService")
@AllArgsConstructor
public class BlogAuditUserConfigServiceImpl extends ServiceImpl<BlogAuditUserConfigDao, BlogAuditUserConfigEntity>
        implements BlogAuditUserConfigService {

    private static final Logger log = LoggerFactory.getLogger(BlogAuditUserConfigServiceImpl.class);

    @Override
    public void saveOrEdit(BlogAuditUserConfigEntity blogAuditUserConfig) {
        // 获取操作用户
        String username = SecurityUtils.getOrDefaultUsername(StringUtils.EMPTY);
        Long userId = SecurityUtils.getOrDefaultUserId(CommonEnum.ZERO.getLongValue());
        Date date = new Date();

        if (Objects.isNull(blogAuditUserConfig.getId())) {
            blogAuditUserConfig.setDeleteStatus(CommonEnum.ZERO.getValue())
                    .setUpdateTime(date)
                    .setUpdateUser(StringUtils.EMPTY)
                    .setUpdateUserId(CommonEnum.ZERO.getLongValue())
                    .setCreateTime(date)
                    .setCreateUser(username)
                    .setCreateUserId(userId);

            int count = baseMapper.insert(blogAuditUserConfig);
            log.info("保存博客审核用户配置完成。count={}", count);
        } else {
            blogAuditUserConfig.setUpdateTime(date)
                   .setUpdateUser(username)
                   .setUpdateUserId(userId);

            int count = baseMapper.updateById(blogAuditUserConfig);
            log.info("更新博客审核用户配置完成。count={}", count);
        }
    }

    @Transactional
    @Override
    public void batchSave(List<BlogAuditUserConfigEntity> blogAuditUserConfigList) {
        boolean bool = this.saveBatch(blogAuditUserConfigList);
        log.info("批量保存博客审核用户配置完成。bool={}", bool);
    }

    @Override
    public List<BlogAuditUserConfigEntity> findList(List<Long> userIdList) {
        if (CollectionUtils.isEmpty(userIdList)) {
            log.warn("获取博客审核用户配置列表，传递的用户主键列表为空");
            return Collections.emptyList();
        }

        LambdaQueryWrapper<BlogAuditUserConfigEntity> blogAuditUserConfigLambdaQueryWrapper = new LambdaQueryWrapper<>();
        blogAuditUserConfigLambdaQueryWrapper.in(BlogAuditUserConfigEntity::getUserId, userIdList)
                .eq(BlogAuditUserConfigEntity::getDeleteStatus, CommonEnum.ZERO.getValue());
        // 查询
        List<BlogAuditUserConfigEntity> blogAuditUserConfigEntityList = list(blogAuditUserConfigLambdaQueryWrapper);
        log.info("查询博客审核用户配置列表完成。list.size={}", blogAuditUserConfigEntityList.size());
        return blogAuditUserConfigEntityList;
    }

    @Transactional
    @Override
    public void batchUpdate(List<BlogAuditUserConfigEntity> blogAuditUserConfigList) {
        boolean bool = this.updateBatchById(blogAuditUserConfigList);
        log.info("批量更新博客审核用户配置完成。bool={}", bool);
    }

    @Override
    public void updateClaimNum(Long userId, Integer claimNum) {
        // 获取操作用户
        String username = SecurityUtils.getOrDefaultUsername("系统");
        Long optUserId = SecurityUtils.getOrDefaultUserId(CommonEnum.ZERO.getLongValue());
        Date date = new Date();

        // 更新
        int count = baseMapper.updateClaimNum(userId, claimNum, date, username, optUserId);
        log.info("更新博客审核用户认领数量完成。count={}", count);
    }

    @Override
    public void updateAuditNum(Long userId, Integer auditNum) {
        // 获取操作用户
        String username = SecurityUtils.getOrDefaultUsername("系统");
        Long optUserId = SecurityUtils.getOrDefaultUserId(CommonEnum.ZERO.getLongValue());
        Date date = new Date();

        // 更新
        int count = baseMapper.updateAuditNum(userId, auditNum, date, username, optUserId);
        log.info("更新博客审核用户审核数量完成。count={}", count);
    }
}
